package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Employee;
import com.itheima.service.Impl.EmployeeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeServiceImpl employeeService;

//      登录
    @PostMapping("/login")
    public R<Employee> Login(HttpServletRequest request, @RequestBody Employee employee){

        //获取当前运行时用户密码
        String ps = employee.getPassword();

//        hex十六进制   将用户密码进行加密
       ps  =DigestUtils.md5DigestAsHex(ps.getBytes());

       //把  当前用户名放入条件构造器  条件构造器进行
        LambdaQueryWrapper<Employee> lQW = new LambdaQueryWrapper<>();
         lQW.eq(Employee::getUsername, employee.getUsername());
        Employee em = employeeService.getOne(lQW);


//        如果用户名是空登录失败
        if (em==null){
             return R.error("登录失败！");
         }

//         如果运行的密码和数据库的密码不一致
        if (!em.getPassword().equals(ps)){
            return R.error("登录失败！");
        }
//          如果数据库中的状态为0时禁用
        if (em.getStatus()==0){
            return R.error("账号以禁用！");
        }
//          如果登录成功存入session中
        request.getSession().setAttribute("employee",em.getId());
        return R.success(em);
    }
        //退出
    @PostMapping("/logout")
    public R<String> request(HttpServletRequest request){
        //将存入session中的数据
        request.getSession().removeAttribute("employee");
        return R.success("退出成功！");
    }


//      分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name ){
        Page page1 = new Page(page,pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        /*StringUtils.isNotEmpty——不为空！*/
        wrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        employeeService.page(page1,wrapper);
        return R.success(page1);
    }


    //添加
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
//        Long empId = (Long)request.getSession().getAttribute("employee");
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("添加成功");
    }


    //回显
    @GetMapping("/{id}")
    public R<Employee> GetById(@PathVariable Long id){
        Employee byId = employeeService.getById(id);
        if (byId==null){
            return R.error("没查询到该用户");
        }
        return R.success(byId);
    }

//    修改数据：
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee){
//        Long empId = (Long)request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    //删除
    @DeleteMapping
    public R<String> DeletByID(@PathVariable Integer id){
        employeeService.removeById(id);
        return R.success("删除成功！");
    }



}
